<?php
namespace lib;
use lib\{GlobalState, CurlUrl, DB , MsgNotice,Order_Template,Tips};

class BaseController {
   protected $globalState;
   protected $input;
   // 动态加载属性
   public function __get($name) {
        if (!isset($this->$name)) {
            $commonValues = [
                'conf', 'islogin_user', 'islogin_admin', 'httphost',
                'date', 'userrow', 'uid', 'CACHE', 'adminrow', 'salt', 'qqprotocol'
            ];
            
            if (in_array($name, $commonValues)) {
                $this->$name = $this->globalState->get($name);
            } else {
                switch ($name) {
                    case 'db':
                        $this->$name = new DB();
                        break;
                    case 'CurlUrl':
                        $this->$name = new CurlUrl();
                        break;
                    case 'Hang_Token':
                        $this->$name = $this->Hang_Token();
                        break;
                    default:
                        $this->$name = null;
                }
            }
        }
        return $this->$name;
    }

   public function __construct() {
       $this->globalState = GlobalState::getInstance();
       $this->input = (object)array_merge(
           (array)input(),
           (array)input("input")
       );
       $this->authdata = [
           'url'=>"https://auth.daoh.top",
           'cxid'=>2,
           'host'=> defined('xfhost') && !empty(xfhost) ? xfhost : $_SERVER["HTTP_HOST"],
           'authcode'=>dbconfig['xfmzfsqm']
        ];
       $this->MsgNotice = new MsgNotice($this->globalState->get('conf'));
       $this->Order_Template = new Order_Template($this->globalState->get('conf'));
       $this->E_Type = [
            'alipay'=>"支付宝",
            'wxpay'=>"微信支付",
            'qqpay'=>"QQ支付",
            'usdt'=>"Usdt支付"
        ];
       
      
   }
   //获取路由 后续可以考虑做 无需在每个方法中 添加 登录效验
   public function ObtainRoute(){
       $requestUri = $_SERVER['REQUEST_URI'];
       $uri = parse_url($requestUri, PHP_URL_PATH);
       $uri = ltrim($uri, '/');
       $parts = explode('/', $uri); // 解析 URI
       return $parts;
   }
   
   public function Hang_Token(){
       return md5('XFHANGGUAJITOKEN1745****47432');
   }
   
   
   public function get_notify($E_type , $srow , $userrow){
       
        $E_type = $E_type[$srow['type']];
        $url=creat_callback($srow);
        $resdata=do_notify($url['notify']);
        $params = [];
        if($resdata){
            $params['notify'] = 0;
        }else{
            $params['notify'] = $row['notify'] + 1;
            $params['notifytime'] = $this->date;
        }
        $this->db->update('order',$params,['id'=>$srow['id']]);
        $data=[
        'user'=>$userrow['user'],
        'paragraphs' => [
                ['label' => 'PID', 'value' => $userrow['pid']],
                ['label' => '订单号', 'value' => $srow['trade_no']],
                ['label' => '收款方式','value'=>$E_type],
                ['label' => '商品名称','value'=>$srow['name']],
                ['label' => '支付金额','value'=>$srow['money']],
                ['label' => '站点地址','value'=>$this->httphost],
                ['label' => '有问题请联系站长QQ','value'=>$this->conf['qq']]
            ]
        ];
        $msg = $this->Order_Template->send(order_emailtemp,$data,'新订单通知');
        if(!empty($msg)){
            $email= send_mail($userrow['email'], $this->conf['sitename'].'- 收款到账提醒', $msg);
        }
        createLog('异步回调成功 ： '.$srow['trade_no'].'',http_ip(),$userrow['user']?:"系统处理");
        return $resdata;
    }
    public function echomsg($name,$msg){
        switch ($name) {
            case 'html':
                echo $msg;
                exit;
                break;
            case 'json':
                exitjson(-1,$msg);
                break;
            case 'Tips':
                Tips::error($msg,'响应错误');
                break;
            default:
                echo $msg;
                exit;
                break;
        }
    }
    public function checkRequiredParams($requiredFields=[] , $data=[]){
        foreach ($requiredFields as $field){
            if (!isset($data[$field]) || empty($data[$field])){
                return true;
                break;
            }
        }
        return false;
    }
    

//   public function cs() {
//       // exit("未授权");
//   }
}